package com.carkeeper.canal.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //定义上架队列名称
    public static final String SEARCH_ADD_QUEUE="search_add_queue";

    //定义下架队列名称
    public static final String SEARCH_DEL_QUEUE="search_del_queue";

    //定义上架交换机
    public static final String SERVICE_UP_EXCHANGE="service_up_exchange";

    //定义下架交换机
    public static final String SERVICE_DOWN_EXCHANGE="service_down_exchange";

    //声明上架队列
    @Bean(SEARCH_ADD_QUEUE)
    public Queue SEARCH_ADD_QUEUE(){
        return new Queue(SEARCH_ADD_QUEUE);
    }

    //声明下架队列
    @Bean(SEARCH_DEL_QUEUE)
    public Queue SEARCH_DEL_QUEUE(){
        return new Queue(SEARCH_DEL_QUEUE);
    }

    //声明上架交换机
    @Bean(SERVICE_UP_EXCHANGE)
    public Exchange SERVICE_UP_EXCHANGE(){
        return ExchangeBuilder.fanoutExchange(SERVICE_UP_EXCHANGE).durable(true).build();
    }

    //声明下架交换机
    @Bean(SERVICE_DOWN_EXCHANGE)
    public Exchange SERVICE_DOWN_EXCHANGE(){
        return ExchangeBuilder.fanoutExchange(SERVICE_DOWN_EXCHANGE).durable(true).build();
    }

    //上架队列绑定到上架交换机
    @Bean
    public Binding AD_UPDATE_QUEUE_BINDING(@Qualifier(SEARCH_ADD_QUEUE)Queue queue,
                                           @Qualifier(SERVICE_UP_EXCHANGE)Exchange exchange){
        //不适用路由k
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    //下架队列绑定到下架交换机中
    @Bean
    public Binding GOODS_DOWN_EXCHANGE_BINDING(@Qualifier(SEARCH_DEL_QUEUE) Queue queue,
                                               @Qualifier(SERVICE_DOWN_EXCHANGE) Exchange exchange){
        //不适用routinkey路由key
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
